python section and sys.argv[

  • 2020-09-16 07:37:31
  • OfStack

1. python slice


a=a[::-1]  Reverse order 
a=a[1:10:2]  The subscript 1~10 In order to 2 Interval from 
a=a[::2] 2 Interval return a[:]
a=[1,2,3,4,5,6,7]
print(a[::2])
[1,3,5,7]

2. sys. argv [] usage

Sys.argv [] is used to get command line arguments. sys.argv [0] represents the file path of the code itself, so the arguments start at 1.

Ex. :


import sys,os
os.system(sys.argv[1])

This example, os.system, receives the command line arguments, runs the argument instruction, saves it as ES20en1.py, and runs ES22en1.py notepad from the command line with the arguments, which opens notepad.

In the same directory as ES27en.py, create a new notepad file of ES29en.txt, which contains the following contents: test argv; sample. py a. txt, output a. txt file content: test argv, here can also take a few more parameters, the program will output the parameter file content.

conclusion


Related articles: